package com.zach.common.servlet;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.zach.util.ConfigUtil;
/**
 * 环境变量初始化Servlet<br/>
 * 目前主要用于网站关键字等项的配置
 * @author john
 *
 */
public class EnvironmentInitialServlet extends HttpServlet {
	private static final long serialVersionUID = -6622309232705162847L;
	private static Log log = LogFactory.getLog(EnvironmentInitialServlet.class);
	public EnvironmentInitialServlet() {
		super();
	}
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		destroyWebEnv();
	}

	public void init() throws ServletException {
		initWebEnv();
	}
	
	private void initWebEnv(){
		log.info("初始化网站变量配置.....");
		String section = "site";
		String keywords = ConfigUtil.getConfValue(section, "keywords");
		String title = ConfigUtil.getConfValue(section, "title");
		String description = ConfigUtil.getConfValue(section, "description");
		ServletContext ctx =  getServletContext();
		ctx.setAttribute("site_keywords", keywords);
		ctx.setAttribute("site_title", title);
		ctx.setAttribute("site_description", description);
		StringBuilder sb = new StringBuilder();
		sb.append("环境变量配置：\n\tkeywords=").append(keywords).append("\n\t")
			.append("title=").append(title).append("\n\t")
			.append("description=").append(description);
		log.debug(sb.toString());
	}
	public void destroyWebEnv(){
		ServletContext ctx =  getServletContext();
		log.debug("移除变量配置");
		ctx.removeAttribute("site_title");
		ctx.removeAttribute("site_keywords");
		ctx.removeAttribute("site_description");
	}
}
